package com.hand.demo.api.controller.v1;

import com.hand.demo.domain.entity.FileUpload;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

import org.codehaus.jackson.map.ObjectMapper;
import org.hzero.boot.file.FileClient;
import org.hzero.boot.message.MessageClient;
import org.hzero.boot.message.entity.Message;
import org.hzero.boot.message.entity.Receiver;
import org.hzero.boot.platform.code.builder.CodeRuleBuilder;
import org.hzero.boot.platform.profile.ProfileClient;
import org.hzero.core.base.BaseController;
import org.hzero.core.util.Results;
import com.hand.demo.app.service.ExampleService;
import com.hand.demo.config.SwaggerTags;
import com.hand.demo.domain.entity.Example;
import com.hand.demo.domain.repository.ExampleRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * API接口
 */
@Api(tags = SwaggerTags.EXAMPLE)
@RestController("exampleController.v1")
@RequestMapping("/v1/example")
public class ExampleController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(ExampleController.class);
    @Autowired
    private ExampleService exampleService;
    @Autowired
    private ExampleRepository exampleRepository;
    @Autowired
    private CodeRuleBuilder codeRuleBuilder;
    @Autowired
    private ProfileClient profileClient;
    @Autowired
    private MessageClient messageClient;
    @Autowired
    private FileClient fileClient;

    /**
     * 查询方法
     * 
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID获取")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true) // site平台级权限
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "ID", paramType = "path")})
    @GetMapping("/{id}")
    public ResponseEntity<Example> hello(@PathVariable Long id) {
        return Results.success(exampleRepository.selectByPrimaryKey(id));
    }

    /**
     * 新增方法
     * 
     * @param example
     * @return
     */
    @ApiOperation(value = "新增方法")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @PostMapping("/add")
    public ResponseEntity<?> add(@RequestBody Example example) {
        return Results.success(exampleRepository.insert(example));
    }

    /**
     * 修改方法
     * 
     * @param example
     * @return
     */
    @ApiOperation(value = "修改方法")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @PutMapping("/update")
    public ResponseEntity<?> update(@RequestBody Example example) {
        return Results.success(exampleRepository.updateByPrimaryKeySelective(example));
    }

    /**
     * 删除方法
     * 
     * @param id
     * @return
     */
    @ApiOperation(value = "删除方法")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<?> delete(@PathVariable("id") Long id) {
        Example example = new Example();
        example.setId(id);
        return Results.success(exampleRepository.deleteByPrimaryKey(example));
    }

    /**
     * 自动生成编码规则 ruleCode为编码规则
     * 
     * @return
     */
    @ApiOperation(value = "根据编码自动生成")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @GetMapping("/getCode")
    public ResponseEntity<?> getCode() {
        String code = codeRuleBuilder.generateCode("44200-DEME", null);
        logger.info("{}", code);
        return Results.success(code);
    }

    /**
     * 获取配置维护的值
     * 
     * @return
     */
    @ApiOperation(value = "获取配置维护的值")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @GetMapping("/getVlaue")
    public ResponseEntity<?> getValue() {
        String profileValueByOptions = profileClient.getProfileValueByOptions(0L, null, null, "DEMO-44200");
        return Results.success(profileValueByOptions);
    }

    /**
     * 上传文件
     * 
     * @param fileUpload
     * @return
     */
    @ApiOperation(value = "上传文件")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @PostMapping("/fileUpload")
    public ResponseEntity<?> fileUpload(@RequestBody FileUpload fileUpload) {
        // 按实际情况填充文件字节流
        byte[] bytes = new byte[1024];
        String file = fileClient.uploadAttachment(fileUpload.getOrganization(), fileUpload.getBucketName(),
                        fileUpload.getDirectory(), fileUpload.getAttachmentUUID(), fileUpload.getFileName(),
                        fileUpload.getFileType(), fileUpload.getStorageCode(), bytes);
        return Results.success(file);
    }
    /**
     * 指定接收组发送消息
     *
     * @param tenantId 租户id
     * @param messageTemplateCode 消息模板编码
     * @param receiverGroupCode 指定接收人的用户ID，以及目标租户ID
     * @param args 消息模板参数
     * @return
     */
    /*
     * @PostMapping("/sendMessage") public ResponseEntity<?> sendWebMessage(long tenantId, String
     * messageTemplateCode, String receiverGroupCode, Map<String, String> args) { // 调用方法发送消息 Message
     * message = messageClient.sendWebMessage(tenantId, messageTemplateCode, receiverGroupCode, args);
     * return Results.success(message); }
     */



}
